cmake_minimum_required(VERSION 2.6.2)

# CMP0011 should be set here, otherwise policy set in module won't affect
# here.
CMAKE_POLICY(SET CMP0011 OLD)

# Default CMAKE_INSTALL_PREFIX should be set before PROJECT()
SET(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "Install dir prefix")

####################################################################
# Project specific information
#
PROJECT(common-tts NONE)
SET(PRJ_SUMMARY "A common interface for text-to-speech programs to use speech synthesizers and voice files")

SET(PRJ_DESCRIPTION
"This project aims to be the cross-platform common interface for
text-to-speech (TTS) programs to utilize speech synthesizers and voice
files from different sources and in different directory structures."
   )

SET(AUTHORS "Ding-Yi Chen")
SET(MAINTAINER "dchen@redhat.com")
SET(VENDOR "Red Hat")
SET(LICENSE "BSD")



# Get version from ChangeLog
EXECUTE_PROCESS(COMMAND grep --max-count 1 -E "[[:digit:]]+\\.[[:digit:]]+\\.[[:digit:]]+\$" ChangeLog
    COMMAND sed -e "s/.*-\\s*//"
    OUTPUT_VARIABLE PRJ_VER
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
#MESSAGE("PRJ_VER=${PRJ_VER}")

####################################################################
# Variables
#

SET(USER_BASE_DIR ".config"
    CACHE PATH "User Setting directory"
    )

SET(SYS_BASE_DIR "${CMAKE_INSTALL_PREFIX}"
    CACHE PATH "System base directory"
    )

IF(CMAKE_SYSTEM_PROCESSOR MATCHES "64")
    SET(IS_64 "64" CACHE STRING "IS_64")
ELSE(CMAKE_SYSTEM_PROCESSOR MATCHES "64")
    SET(IS_64 "")
ENDIF(CMAKE_SYSTEM_PROCESSOR MATCHES "64")

SET(SYSTEM_DOC_DIR "${SYS_BASE_DIR}/share/doc"
    CACHE PATH "System Document directory")

SET(COMMON_TTS_DOC_DIR "${SYSTEM_DOC_DIR}/${PROJECT_NAME}"
    CACHE PATH "Common TTS Document directory")

SET(SYSTEM_LIB_DIR "${SYS_BASE_DIR}/lib${IS_64}"
    CACHE PATH "System Library directory")

SET(COMMON_TTS_LIB_DIR "${SYSTEM_LIB_DIR}/${PROJECT_NAME}"
    CACHE PATH "Common TTS Library directory")

SET(SYS_BIN_DIR "${SYS_BASE_DIR}/bin"
    CACHE PATH "System Binary directory")

SET(SYS_CONF_DIR "/etc"
    CACHE PATH "System Config directory")

## Set directory variable for both system and user
MACRO(SET_SYS_USER_DIRS name comment)
    SET(_key "")
    SET(SYS_DIR "")
    SET(USER_DIR "")
    SET(SUB_DIR "")
    FOREACH(_arg ${ARGN})
	IF(_key STREQUAL "")
	    SET(_key "${_arg}")
	ELSE(_key STREQUAL "")
	    SET(${_key} "${_arg}")
	    SET(_key "")
	ENDIF(_key STREQUAL "")
    ENDFOREACH(_arg ${ARGN})

    IF(SYS_DIR STREQUAL "")
	SET(SYS_DIR "${SYS_BASE_DIR}/share/${PROJECT_NAME}")
    ENDIF(SYS_DIR STREQUAL "")
    IF(NOT SUB_DIR STREQUAL "")
	SET(SYS_DIR "${SYS_DIR}/${SUB_DIR}")
    ENDIF(NOT SUB_DIR STREQUAL "")

    IF(USER_DIR STREQUAL "")
	SET(USER_DIR "${USER_BASE_DIR}/${PROJECT_NAME}")
    ENDIF(USER_DIR STREQUAL "")
    IF(NOT SUB_DIR STREQUAL "")
	SET(USER_DIR "${USER_DIR}/${SUB_DIR}")
    ENDIF(NOT SUB_DIR STREQUAL "")

    SET(COMMON_TTS_SYS_${name} "${SYS_DIR}"
	CACHE PATH "System ${comment}")

    SET(COMMON_TTS_USER_${name} "${USER_DIR}"
	CACHE PATH "User ${comment}")
ENDMACRO(SET_SYS_USER_DIRS name comment)

SET_SYS_USER_DIRS(DATA_DIR "Data directory")

SET_SYS_USER_DIRS(VSRC_DIR "Voice source directory"
    SUB_DIR "sources"
    )

SET_SYS_USER_DIRS(CONF_DIR "Config directory" SYS_DIR "${SYS_CONF_DIR}")

SET_SYS_USER_DIRS(BACK_DIR "Binding directory" 
    SUB_DIR "backends" SYS_DIR "${SYS_CONF_DIR}")

SET_SYS_USER_DIRS(BIND_DIR "Binding directory" 
    SUB_DIR "bindings" SYS_DIR "${SYS_CONF_DIR}")


####################################################################
# Building
#
SET(PATH_RC "${CMAKE_BINARY_DIR}/etc/path.rc")
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/etc/path.rc.in ${PATH_RC} @ONLY)

SET(CTTS_FIND "${CMAKE_BINARY_DIR}/bin/ctts-find")
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/bin/ctts-find.in ${CTTS_FIND} @ONLY)

####################################################################
# Installing
#
SET(INSTALL_DOCS ${RELEASE_NOTES_FILE} README ChangeLog COPYING)

INSTALL(FILES ${INSTALL_DOCS}
    DESTINATION "${COMMON_TTS_DOC_DIR}")

INSTALL(PROGRAMS ${CMAKE_SOURCE_DIR}/bin/ctts-player
    ${CMAKE_SOURCE_DIR}/bin/ctts-config-source
    ${CMAKE_SOURCE_DIR}/bin/ctts-prouncer
    ${CMAKE_SOURCE_DIR}/bin/ctts-sayer
    ${CTTS_FIND}
    DESTINATION ${SYS_BIN_DIR})

INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/sources
    DESTINATION ${COMMON_TTS_SYS_DATA_DIR}
    )

INSTALL(PROGRAMS ${PATH_RC}
    DESTINATION ${COMMON_TTS_SYS_CONF_DIR}
    )

INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/etc/backends ${CMAKE_SOURCE_DIR}/etc/bindings
    DESTINATION ${COMMON_TTS_SYS_CONF_DIR}
    )

# uninstall target
CONFIGURE_FILE(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY
    )

ADD_CUSTOM_TARGET(uninstall
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
    )


####################################################################
# Packing
#

SET(PRJ_NV ${PROJECT_NAME}-${PRJ_VER})
SET(PRJ_TARBALL ${PRJ_NV}.tar.gz)

ADD_CUSTOM_TARGET(dist
    COMMAND git archive --prefix ${PRJ_NV}/ --format tar --verbose -o ${PRJ_NV}.tar HEAD
    COMMAND rm -fr ${PRJ_NV}
    COMMAND mkdir -p ${PRJ_NV}/etc
    COMMAND cp ${PATH_RC} ${PRJ_NV}/etc
    COMMAND tar --verbose  --append --file ${PRJ_NV}.tar ${PRJ_NV}/etc/*
    COMMAND bzip2 --force --best ${PRJ_NV}.tar
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    )

####################################################################
# Test Suites.
#
#ENABLE_TESTING()
#ADD_TEST("Test" test_cmd1)

